package com.atguigu.tms.mock.service.impl;

import com.atguigu.tms.mock.bean.*;
import com.atguigu.tms.mock.constant.TmsConstant;
import com.atguigu.tms.mock.mapper.TransportTaskMapper;
import com.atguigu.tms.mock.service.*;
import com.atguigu.tms.mock.service.adv.impl.AdvServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 运输任务表 服务实现类
 * </p>
 *
 * @author zhangchen
 * @since 2022-06-26
 */
@Service
public class TransportTaskServiceImpl extends AdvServiceImpl<TransportTaskMapper, TransportTask> implements TransportTaskService {

    @Autowired
    EmployeeInfoService employeeInfoService;

    @Autowired
    LineBaseInfoService lineBaseInfoService;

    @Autowired
    TransportTaskDetailService transportTaskDetailService;

    @Autowired
    TruckInfoService truckInfoService;


    public void initTransportTask(LineBaseShift lineBaseShift, List<Long> orderIdList, Date curDate) {
        TransportTask transportTask = new TransportTask();

        transportTask.setShiftId(lineBaseShift.getId());

        transportTask.setDriver1EmpId(lineBaseShift.getDriver1EmpId());
        transportTask.setDriver2EmpId(lineBaseShift.getDriver2EmpId());
        EmployeeInfo driver1EmpInfo = employeeInfoService.getById(lineBaseShift.getDriver1EmpId(), true);

        if (lineBaseShift.getDriver2EmpId() != null) {
            EmployeeInfo driver2EmpInfo = employeeInfoService.getById(lineBaseShift.getDriver2EmpId(), true);
            transportTask.setDriver2EmpId(driver2EmpInfo.getId());
            transportTask.setDriver2Name(driver2EmpInfo.getRealName());
        }


        transportTask.setDriver1EmpId(driver1EmpInfo.getId());
        transportTask.setDriver1Name(driver1EmpInfo.getRealName());

        LineBaseInfo lineBaseInfo = lineBaseInfoService.getById(lineBaseShift.getLineId(), true);

        transportTask.setLineId(lineBaseInfo.getId());
        transportTask.setStartOrgId(lineBaseInfo.getStartOrgId());
        transportTask.setStartOrgName(lineBaseInfo.getStartOrgName());
        transportTask.setEndOrgId(lineBaseInfo.getEndOrgId());
        transportTask.setEndOrgName(lineBaseInfo.getEndOrgName());

        Long truckId = lineBaseShift.getTruckId();
        TruckInfo truckInfo = truckInfoService.getById(truckId, true);
        transportTask.setTruckId(truckId);
        transportTask.setTruckNo(truckInfo.getTruckNo());



        transportTask.setCreateTime(curDate);
        transportTask.setIsDeleted("0");

        if (orderIdList != null && orderIdList.size() > 0) {
            List<TransportTaskDetail> transportTaskDetailList = genTransportTaskDetailList(orderIdList);
            transportTask.setOrderNum(transportTaskDetailList.size());

            transportTask.setTransportTaskDetailList(transportTaskDetailList);

        }else{
            transportTask.setOrderNum(0);
        }


        if(transportTask.getTransportTaskDetailList().size()==0){
            transportTask.setStatus(TmsConstant.TRANSPORT_TASK_STATUS_SORTED);  //如果没有货物直接跳过分拣
        }else{
            transportTask.setStatus(TmsConstant.TRANSPORT_TASK_STATUS_ASSIGNED);
        }

        this.saveOrUpdate(transportTask, true); //保存

        if (transportTask.getTransportTaskDetailList().size()> 0) {
            for (TransportTaskDetail transportTaskDetail : transportTask.getTransportTaskDetailList()) {
                transportTaskDetail.setTransportTaskId(transportTask.getId());
                transportTaskDetail.setCreateTime(transportTask.getCreateTime());
            }
            transportTaskDetailService.saveOrUpdateBatch(transportTask.getTransportTaskDetailList(), 100, true);
        }

    }
        private List<TransportTaskDetail> genTransportTaskDetailList (List < Long > lineOrderList) {
            List<TransportTaskDetail> transportTaskDetailList = new ArrayList<>();
            for (Long orderId : lineOrderList) {
                TransportTaskDetail transportTaskDetail = new TransportTaskDetail();
                transportTaskDetail.setOrderId(orderId);
                transportTaskDetail.setIsDeleted("0");
                transportTaskDetailList.add(transportTaskDetail);
            }
            return transportTaskDetailList;
        }


        @Override
        public void loadCache () {
            super.loadCache();
            Map<Long, TransportTaskDetail> transportTaskDetailMap = transportTaskDetailService.all(true);
            for (TransportTaskDetail transportTaskDetail : transportTaskDetailMap.values()) {
                TransportTask transportTask = getById(transportTaskDetail.getTransportTaskId(), true);
                transportTask.getTransportTaskDetailList().add(transportTaskDetail);
            }
        }

}
